/**
 *  StaDiUM - State Diagram Unified Modeler
 *  Copyright (C) 2007  Stefan Bucur, Mihai Balan, Claudiu Tanase
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 *  File Information
 *
 *  Last Modified On: $Date: 2007-06-14 07:08:23 +0000 (Thu, 14 Jun 2007) $
 *  Last Modified By: $Author: stefan.bucur $
 */
package ro.pub.cs.stadium.document;

import java.util.Hashtable;
import java.util.Map;

/**
 * @author stefan
 *
 */
public abstract class AbstractTransition implements ITransition {
	
	private IState startPoint = null;
	private IState endPoint = null;
	
	private String name = "";
	
	private Map<String, String> attributes = 
		new Hashtable<String, String>();

	public IState getEndPoint() {
		return endPoint;
	}
	
	public void setEndPoint(IState state) {
		if (this.endPoint != null)
			this.endPoint.getInTransitions().remove(this.name);
		
		this.endPoint = state;
		
		if (state != null)
			this.endPoint.getInTransitions().put(this.name, this);
	}

	public IState getStartPoint() {
		return startPoint;
	}
	
	public void setStartPoint(IState state) {
		if (this.startPoint != null)
			this.startPoint.getOutTransitions().remove(this.name);
		
		this.startPoint = state;
		
		if (state != null)
			this.startPoint.getOutTransitions().put(this.name, this);
	}

	public String getAttribute(String name) {
		return attributes.get(name);
	}
	
	public void setAttribute(String name, String value) {
		attributes.put(name, value);	
	}
	
	

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.IDocumentItem#getAttributes()
	 */
	public Map<String, String> getAttributes() {
		return attributes;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
		
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.ITransition#getLabelSize()
	 */
	public Size getLabelSize() {
		String cs = getAttribute("labelsize");
		
		if (cs != null)
			return Size.parseSize(getAttribute("labelsize"));
		else
			return null;
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.ITransition#setLabelSize(ro.pub.cs.stadium.document.Size)
	 */
	public void setLabelSize(Size size) {
		if (size == null)
			throw new NullPointerException("size is null");
		
		setAttribute("labelsize", size.toString());
		
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.ITransition#getEndControlPoint()
	 */
	public Point getEndControlPoint() {
		String endCP = getAttribute("endcontrol");
		
		if (endCP != null) {
			return Point.parsePoint(endCP);
		} else
			return null;
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.ITransition#getStartControlPoint()
	 */
	public Point getStartControlPoint() {
		String startCP = getAttribute("startcontrol");
		
		if (startCP != null) {
			return Point.parsePoint(startCP);
		} else 
			return null;
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.ITransition#setEndControlPoint(ro.pub.cs.stadium.document.Point)
	 */
	public void setEndControlPoint(Point point) {
		if (point == null)
			throw new NullPointerException("point is null");
		
		setAttribute("endcontrol", point.toString());
		
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.ITransition#setStartControlPoint(ro.pub.cs.stadium.document.Point)
	 */
	public void setStartControlPoint(Point point) {
		if (point == null)
			throw new NullPointerException("point is null");
		
		setAttribute("startcontrol", point.toString());
		
	}

	
}
